/*
 * ApplicationAView.java
 */

package applicationa;

import applicationa.emulatorMessages.ConsumerMessages;
import applicationa.emulatorMessages.MessageMonitor;
import applicationa.emulatorMessages.ProductorMessages;
import org.apache.log4j.Logger;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;

/**
 * The application's main frame.
 */
public class ApplicationAView extends FrameView {

    public ApplicationAView(SingleFrameApplication app) {
        super(app);

        initComponents();
        
        MessageMonitor monitor = new MessageMonitor();
        this.prod = new ProductorMessages(monitor);
        this.con = new ConsumerMessages(monitor);

        this.prod.start();
        this.con.start();
    }

  

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        codProvTxt = new javax.swing.JTextField();
        codProveedorLbl = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mensajeTxt = new javax.swing.JTextArea();
        mensajeLbl = new javax.swing.JLabel();
        doneButton = new javax.swing.JButton();
        msgLbl = new javax.swing.JLabel();
        cliLabel = new javax.swing.JLabel();
        cliTxt = new javax.swing.JTextField();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(applicationa.ApplicationA.class).getContext().getResourceMap(ApplicationAView.class);
        codProvTxt.setText(resourceMap.getString("codProvTxt.text")); // NOI18N
        codProvTxt.setName("codProvTxt"); // NOI18N

        codProveedorLbl.setText(resourceMap.getString("codProveedorLbl.text")); // NOI18N
        codProveedorLbl.setName("codProveedorLbl"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        mensajeTxt.setColumns(20);
        mensajeTxt.setRows(5);
        mensajeTxt.setName("mensajeTxt"); // NOI18N
        jScrollPane1.setViewportView(mensajeTxt);

        mensajeLbl.setText(resourceMap.getString("mensajeLbl.text")); // NOI18N
        mensajeLbl.setName("mensajeLbl"); // NOI18N

        doneButton.setText(resourceMap.getString("doneButton.text")); // NOI18N
        doneButton.setName("doneButton"); // NOI18N
        doneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doneButtonActionPerformed(evt);
            }
        });

        msgLbl.setText(resourceMap.getString("msgLbl.text")); // NOI18N
        msgLbl.setName("msgLbl"); // NOI18N

        cliLabel.setText(resourceMap.getString("cliLabel.text")); // NOI18N
        cliLabel.setName("cliLabel"); // NOI18N

        cliTxt.setText(resourceMap.getString("cliTxt.text")); // NOI18N
        cliTxt.setName("cliTxt"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(doneButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cliLabel)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(codProveedorLbl)
                                    .addComponent(mensajeLbl))))
                        .addGap(11, 11, 11)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addComponent(msgLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                                    .addComponent(codProvTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                                    .addComponent(cliTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))))))
                .addGap(28, 28, 28))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cliLabel)
                    .addComponent(cliTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(codProvTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(doneButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(msgLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(codProveedorLbl)
                        .addGap(18, 18, 18)
                        .addComponent(mensajeLbl)))
                .addGap(38, 38, 38))
        );

        setComponent(mainPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed

        String entireMsg = this.cliTxt.getText() + "-" + this.codProvTxt.getText() + "-" + this.mensajeTxt.getText();

        this.prod.getMessageList().add(entireMsg);

        //Registro el mensaje en los logs correspondientes
        this.logger.info("Llamada Entrante del cliente: " + this.cliTxt.getText() + " al proveedor: " +
                this.codProvTxt.getText());



        if(this.prod.getMessageList().size() == MessageMonitor.maxMessage){
//            this.prod.start();
//            this.con.start();
            this.msgLbl.setText("Mensajes enviados a central teléfonica.");
        }else{
            this.msgLbl.setText("Mensaje ingresado en buffer teléfonico.");
        }

    }//GEN-LAST:event_doneButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel cliLabel;
    private javax.swing.JTextField cliTxt;
    private javax.swing.JTextField codProvTxt;
    private javax.swing.JLabel codProveedorLbl;
    private javax.swing.JButton doneButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel mensajeLbl;
    private javax.swing.JTextArea mensajeTxt;
    private javax.swing.JLabel msgLbl;
    // End of variables declaration//GEN-END:variables

    private ProductorMessages prod;
    private ConsumerMessages con;

    private Logger logger =Logger.getLogger(ApplicationAView.class);
    

}
